﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using AutoMapper;
using GPS.DataAccessLayer;
using Microsoft.AspNet.Identity;

namespace GPS.Controllers
{
    public abstract class GPS_BaseController<T, M> : Controller
        where M : new()
        where T : class
    {
        protected IGenericRepository<T> _repository;
        public GPS_BaseController(IGenericRepository<T> repository)
        {
            this._repository = repository;
            //ViewBag.CurrentUser = CurrentUser;
        }

        public GPS_BaseController()
        {
          //  this._repository = repository;
           // ViewBag.CurrentUser = CurrentUser;
        }

        public ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();

            string userId = User.Identity.GetUserId();

            ApplicationUser _currentUser = dbUser.Users.Find(userId);

            return _currentUser;
        }
        public ApplicationUser CurrentUser
        {
            get
            {
                ApplicationUser _currentUser = getCurrentUser();
                return _currentUser;
            }
        }

        protected virtual int PageSize
        {
            get { return 5; }
        }

        public virtual ActionResult Index(int? currentPage)
        {
            var entities = _repository.SelectAll();
            List<M> model = new List<M>();

            foreach (var currentEntity in entities)
            {
                model.Add(Mapper.Map<M>(currentEntity));
            }

            int pageNumber = (currentPage ?? 1);
            return View(model.ToPagedList(pageNumber, PageSize));
        }

        [HttpGet]
        public virtual ActionResult Create()
        {
            return View(new M());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public virtual ActionResult Create(M model)
        {
            if (ModelState.IsValid)
            {
                _repository.Insert(Mapper.Map<T>(model), CurrentUser);
                return RedirectToAction("Index");
            }

            return View(model);
        }

        [HttpGet]
        public virtual ActionResult Edit(int modelId)
        {
            T domainModelEntity = _repository.SelectById(modelId);
            M model = Mapper.Map<M>(domainModelEntity);

            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public virtual ActionResult Edit(M model)
        {
            if (ModelState.IsValid)
            {
                _repository.Update(Mapper.Map<T>(model), CurrentUser);
                return RedirectToAction("Index");
            }

            return View(model);
        }

        public virtual ActionResult Delete(int modelId)
        {
            _repository.Delete(modelId);
            return RedirectToAction("Index");
        }
    }

    //sử dụng kế thừa
    public class WebPagesController : GPS_BaseController<SQUOCTICH, SQUOCTICH>
    {
        public WebPagesController(GPS_DANHMUC_Repository<SQUOCTICH> repository)
            : base(repository)
        {
        }
    }
}

